﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;
using VietThuongLMS.Utils;

/**
 * <Jason>
 * <Date: 20131114>
 * 
 */
namespace VietThuongLMS.Services
{
    public class LopHocService
    {

        public List<LopHoc> GetLopHocList()
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                List<LopHoc> list = context.LopHocs.Where(m => m.Deleted == Utils.Constant.INACTIVE).ToList();
                //List<LopHoc> list = context.LopHocs.Include("LoaiTaiKhoan").Where(m => m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_TEACHER).ToList();
                return list;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::GetLopHocList", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static LopHocViewModel GetLopHocList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                List<LopHoc> list = context.LopHocs.Where(m => m.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL).OrderBy(m => m.NgayKhaiGiang).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                foreach (LopHoc lh in list)
                {
                    lh.LoaiLopHocReference.Load();
                    lh.MonHocReference.Load();
                    lh.ChiNhanhReference.Load();
                }
                int total = context.LopHocs.Where(m => m.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL).Count();
                return new LopHocViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::GetLopHocList", ex.StackTrace, ex.Message);
                return new LopHocViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }
        
        public static LopHocViewModel GetLopHocList(int pageNum, int pageIndex, string userName)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                TaiKhoan tk = context.TaiKhoans.FirstOrDefault(t => t.MaTaiKhoan == userName);
                NhanVien nhanVien = context.NhanViens.FirstOrDefault(n => n.MaTaiKhoan == tk.ID);

                List<LopHoc> list = context.LopHocs.Where(m => m.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL
                        && m.MaChiNhanh == nhanVien.MaChiNhanh).OrderBy(m => m.NgayKhaiGiang).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                foreach (LopHoc lh in list)
                {
                    lh.LoaiLopHocReference.Load();
                    lh.MonHocReference.Load();
                    lh.ChiNhanhReference.Load();
                }
                int total = context.LopHocs.Where(m => m.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL).Count();
                return new LopHocViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::GetLopHocList", ex.StackTrace, ex.Message);
                return new LopHocViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertLopHoc(LopHoc comp)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                context.AddToLopHocs(comp);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::InsertLopHoc", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static int InsertLopHocReturn(LopHoc comp)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                context.AddToLopHocs(comp);
                context.SaveChanges();
                return comp.ID;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::InsertLopHoc", ex.StackTrace, ex.Message);
                return Constant.RETURNCODE_ERROR;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteLopHoc(int id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                LopHoc obj = context.LopHocs.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    obj.Deleted = Utils.Constant.GENERAL_STATUS_DELETED;
                    //context.LopHocs.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::DeleteLopHoc", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static LopHoc GetLopHocById(int id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                return context.LopHocs.Where(c => c.ID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::GetLopHocById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(LopHoc newModel)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                LopHoc model = context.LopHocs.Where(c => c.ID == newModel.ID).FirstOrDefault();

                if (model != null)
                {
                    model.TenLopHoc = newModel.TenLopHoc;
                    //model.NgayKhaiGiang = newModel.NgayKhaiGiang;
                    //model.NgayKetThuc = newModel.NgayKetThuc;
                    model.NgonNgu = newModel.NgonNgu;
                    //model.MaLoaiLopHoc = newModel.MaLoaiLopHoc;
                    //model.MaMonHoc = newModel.MaMonHoc;
                    model.TinhTrang = newModel.TinhTrang;
                    model.MaChiNhanh = newModel.MaChiNhanh;
                    model.SoLuong = newModel.SoLuong;

                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        //authenticate
        public static LopHoc GetLopHoc(string tenLopHoc)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.LopHocs.Single(a => a.TenLopHoc.Equals(tenLopHoc) && a.Deleted != Constant.GENERAL_STATUS_DELETED);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::GetLopHoc", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Lay danh sach lop hoc con thieu hoc vien
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static LopHocAvailableViewModel GetLopHocAvailableList(int hocVienId, int pageNum, int pageIndex)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                //lay thong tin hoc vien
                HocVien_LopMoRongModel hocVienLop = context.HocVien_Lop
                    .Join(context.HocViens, hl => hl.MaHocVien, h => h.ID, (hl, h) => new { HocVien_Lop = hl, HocVien = h })
                    .Join(context.TaiKhoans, hv => hv.HocVien.MaTaiKhoan, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = t })
                    .Where(s => s.HocVien_Lop.Deleted == Constant.GENERAL_STATUS_NORMAL && s.HocVien_Lop.ID == hocVienId)
                    .OrderByDescending(s => s.HocVien_Lop.NgayDangKy)
                    //.Skip(pageNum * (pageIndex - 1)).Take(pageNum)
                    .Select(a => new HocVien_LopMoRongModel
                    {
                        HV_L = a.HocVien_Lop,
                        HoTen = a.TaiKhoan.HoTen,
                        DienThoai = a.TaiKhoan.DienThoai,
                        TenLoaiLopHoc = a.HocVien_Lop.LoaiLopHoc.TenLoaiLopHoc,
                        ChuyenDe = a.HocVien_Lop.ChuyenDe,
                        TinhTrang = a.HocVien_Lop.Deleted.Value,
                        NgonNgu = a.HocVien_Lop.NgonNgu
                    }).FirstOrDefault();

                //lay danh sach lop hoc
                List<LopHocAvailableModel> list = context.LopHocs.Where(m => m.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL
                    && (m.TinhTrang == Constant.SAP_MO))
                    .Join(context.LoaiLopHocs, l => l.MaLoaiLopHoc, llh => llh.ID, (l, llh) => new { LopHoc = l, LoaiLopHoc = llh })
                    .Join(context.MonHocs, l => l.LopHoc.MaMonHoc, m => m.ID, (l, m) => new { LopHoc = l, MonHoc = m })
                    .OrderBy(m => m.LopHoc.LopHoc.NgayKhaiGiang)
                    .Select(s => new LopHocAvailableModel
                        {
                            Id = s.LopHoc.LopHoc.ID,
                            NgayKhaiGiang = s.LopHoc.LopHoc.NgayKhaiGiang.Value,
                            NgonNgu = s.LopHoc.LopHoc.NgonNgu,
                            TinhTrang = s.LopHoc.LopHoc.TinhTrang,
                            TenLoaiLopHoc = s.LopHoc.LoaiLopHoc.TenLoaiLopHoc,
                            TenLopHoc = s.LopHoc.LopHoc.TenLopHoc,
                            TenMonHoc = s.MonHoc.TenMonHoc
                        }).ToList();
                return new LopHocAvailableViewModel(hocVienLop, list.Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList(), list.Count());
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LopHocServices::LopHocAvailableViewModel", ex.StackTrace, ex.Message);
                return new LopHocAvailableViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static List<LopHocMini> GetDanhSachLopByTaiKhoanId(string maTaiKhoan)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<LopHocMini> hocVienLop = context.HocVien_Lop
                    .Join(context.HocViens, hl => hl.MaHocVien, h => h.ID, (hl, h) => new { HocVien_Lop = hl, HocVien = h })
                    .Join(context.TaiKhoans, hv => hv.HocVien.MaTaiKhoan, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = t })
                    .Join(context.LopHocs, hv => hv.HocVien_Lop.MaLopHoc, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = s.TaiKhoan, LopHoc = t })
                    .Where(s => s.HocVien_Lop.Deleted == Constant.GENERAL_STATUS_NORMAL && s.TaiKhoan.MaTaiKhoan == maTaiKhoan)
                    .Select(a => new LopHocMini
                    {
                        ID = a.LopHoc.ID,
                        TenLopHoc = a.LopHoc.TenLopHoc,
                        MaHocVien = a.HocVien.ID
                    }).ToList();

                    return hocVienLop;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhServices::GetDanhSachLopByTaiKhoanId", ex.StackTrace, ex.Message);
                return new List<LopHocMini>();
            }
        }
    }
}
